package com.xyz.util.dto;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

public class DataResult implements Serializable {

    private static final Logger log = LoggerFactory.getLogger(DataResult.class);

    private static final long serialVersionUID = 1L;

    // 9100 请求成功
    // 9200 参数错误，数据验证出现问题（前端传参错误）
    // 9250 用户操作出错（前端可以直接msg用来提醒用户）
    // 9300 未登录
    // 9400 登录过期
    // 9404 页面或数据不存在
    // 9500 系统繁忙（系统出错）
    private Integer code;

    // 返回参数描述
    private String msg;

    // 返回的具体数据
    private Object info;

    public static DataResult build9100() {
        DataResult dr = new DataResult();
        dr.setCode(9100);
        dr.setMsg("请求成功");
        log.info(dr.toString());
        return dr;
    }

    public static DataResult build9100(Object info) {
        DataResult dr = new DataResult();
        dr.setCode(9100);
        dr.setMsg("请求成功");
        dr.setInfo(info);
        log.info(dr.toString());
        return dr;
    }

    public static DataResult build9200(String msg) {
        DataResult dr = new DataResult();
        dr.setCode(9200);
        if (msg == null) {
            dr.setMsg("传入参数错误");
        } else {
            dr.setMsg(msg);
        }
        log.info(dr.toString());
        return dr;
    }

    public static DataResult build9250(String msg) {
        DataResult dr = new DataResult();
        dr.setCode(9250);
        dr.setMsg(msg);
        log.info(dr.toString());
        return dr;
    }

    public static DataResult build9300() {
        DataResult dr = new DataResult();
        dr.setCode(9300);
        dr.setMsg("你还未登录，请先登录");
        log.info(dr.toString());
        return dr;
    }

    public static DataResult build9400() {
        DataResult dr = new DataResult();
        dr.setCode(9400);
        dr.setMsg("登录已过期，请重新登录");
        log.info(dr.toString());
        return dr;
    }

    public static DataResult build9404() {
        DataResult dr = new DataResult();
        dr.setCode(9404);
        dr.setMsg("页面或数据不存在");
        log.info(dr.toString());
        return dr;
    }

    public static DataResult build8100() {
        DataResult dr = new DataResult();
        dr.setCode(8100);
        dr.setMsg("巡检单状态错误");
        log.info(dr.toString());
        return dr;
    }

    public static DataResult build8101() {
        DataResult dr = new DataResult();
        dr.setCode(8101);
        dr.setMsg("巡检单人ID为空");
        log.info(dr.toString());
        return dr;
    }

    public static DataResult build8102() {
        DataResult dr = new DataResult();
        dr.setCode(8102);
        dr.setMsg("当前巡检单和登陆人不匹配");
        log.info(dr.toString());
        return dr;
    }

    public static DataResult build8103() {
        DataResult dr = new DataResult();
        dr.setCode(8103);
        dr.setMsg("巡检单设备树ID为空");
        log.info(dr.toString());
        return dr;
    }

    public static DataResult build8104() {
        DataResult dr = new DataResult();
        dr.setCode(8104);
        dr.setMsg("该设备不在当前的巡检单范围内");
        log.info(dr.toString());
        return dr;
    }

    public static DataResult build9500() {
        DataResult dr = new DataResult();
        dr.setCode(9500);
        dr.setMsg("系统繁忙，请稍后再试");
        log.info(dr.toString());
        return dr;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getInfo() {
        return info;
    }

    public void setInfo(Object info) {
        this.info = info;
    }

    public String toInfo() {
        switch (code) {
            case 9100:
                return "请求成功";
            case 9250:
                return "请求成功，操作错误";
            case 9300:
            case 9400:
                return "请求失败，未登录";
            case 9200:
            case 9500:
                return "请求失败，系统错误";
            default:
                return "未知";
        }
    }

    @Override
    public String toString() {
        return "DataResult{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", info=" + info +
                '}';
    }

}
